//
//  JSTServer.m
//  JSTMobClick
//
//  Created by Czj on 16/8/28.
//  Copyright © 2016年 Czj. All rights reserved.
//

#import "JSTServer.h"
#import "AES128_DawnzrXie.h"

@implementation JSTServer
//请求广告
+ (void) requestAdvert:(NSString *)advertId
               dataDic:(void (^)(NSDictionary *dataDic))dataDic
              errorStr:(void (^)(NSString *, NSInteger))errorStr
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:advertId forKey:@"id"];
    [dict setValue:[JSTSystemInfo token] forKey:@"token"];
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@", kAppServerForHttps, GetAdvertInterface];
    [JSTHttpClient sendRequest:urlStr requestMethod:AFRequestMethodGet parameters:dict success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if ([[dic valueForKey:@"status"] integerValue] != 1 && [[dic valueForKey:@"status"] integerValue] != 2) {
            if (dataDic) dataDic(nil);
        }else  {
            if (dataDic) dataDic(dic);
        };
    } failure:^(NSUInteger statusCode, NSString *error) {
        errorStr(error, statusCode);
        DLog(@"广告请求失败 err = %@", error);
    }];
}
//提交统计请求
+ (void)requestPostStatisticsData:(NSDictionary *)dict
                    postInterface:(NSString *)postUrl
                          dataDic:(void (^)(NSDictionary *dataDic))dataDic
                         errorStr:(void (^)(NSString *error))errorStr
{
    if (IsStrEmpty(postUrl)) return;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:dict];
    [dic setValue:[JSTSystemInfo token] forKey:@"token"];
    [JSTHttpClient sendRequest:postUrl requestMethod:AFRequestMethodGet parameters:dic success:^(id responseObject)
    {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if (dataDic) dataDic(dic);
    } failure:^(NSUInteger statusCode, NSString *error){
        if (errorStr) errorStr(error);
    }];
}

//推送数据请求
+ (void)requestNotification:(void (^)(NSDictionary *))notifiDic
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[JSTSystemInfo applicationIdentifier] forKey:@"packName"];
    [dict setValue:[JSTSystemInfo userKey] forKey:@"key"];
    [JSTHttpClient sendRequest:[NSString stringWithFormat:@"%@/%@", kAppServerForHttps, NotificaitonInterface] requestMethod:AFRequestMethodGet parameters:dict success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if(notifiDic) notifiDic(dic);
    } failure:^(NSUInteger statusCode, NSString *error) {
        notifiDic(nil);
    }];
}

//通过IP获取地址请求
+ (void) requestAddressData:(void (^)(NSDictionary *addressData))addressDic
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:GDIPA_KEY forKey:@"key"];
    [dict setValue:@"json" forKey:@"output"];
    [JSTHttpClient sendRequest:AddressInterface requestMethod:AFRequestMethodPost parameters:dict success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if (addressDic) addressDic(dic);
    } failure:^(NSUInteger statusCode, NSString *error) {
        if (addressDic) addressDic(nil);
    }];
}

//版本控制接口
+ (void)requestVersionControlData:(void (^)(NSDictionary *))versionDic err:(void (^)(NSString *))errStr
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[JSTSystemInfo userKey] forKey:@"key"];
    [dict setValue:[JSTSystemInfo applicationIdentifier] forKey:@"andriod_apkname"];
    [dict setValue:[JSTSystemInfo currentProjectVersion] forKey:@"edition"];
    [JSTHttpClient sendRequest:[NSString stringWithFormat:@"%@/%@", kAppServerForHttps, VersionControlInterface] requestMethod:AFRequestMethodPost parameters:dict success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if (versionDic) versionDic(dic);
    } failure:^(NSUInteger statusCode, NSString *error) {
        if (errStr) errStr(error);
    }];
}

//推送到达率
+ (void)requestPushArrivalRateData:(NSString *)pushId push:(void (^)(NSDictionary *))pushArrivalRateData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[JSTSystemInfo userKey] forKey:@"key"];
    [dict setValue:[JSTSystemInfo applicationIdentifier] forKey:@"pakname"];
    [dict setValue:pushId forKey:@"push_id"];
    [dict setValue:@"3" forKey:@"browsestatus"];
    [JSTHttpClient sendRequest:[NSString stringWithFormat:@"%@/%@", kAppServerForHttps, PushArrivalRateInterface
                                ] requestMethod:AFRequestMethodPost parameters:dict success:^(id responseObject) {
       NSDictionary *dic = (NSDictionary *)responseObject;
        if (pushArrivalRateData) pushArrivalRateData(dic);
    } failure:^(NSUInteger statusCode, NSString *error) {
        if (pushArrivalRateData) pushArrivalRateData(nil);
    }];
}

//过滤回调接口
+ (void)requestBackFilteredData:(void (^)(NSDictionary *))backFilteredDic err:(void (^)(NSString *))errStr
{
    NSUserDefaults *userDf = [NSUserDefaults standardUserDefaults];
    NSArray *arr = [userDf valueForKey:@"filteredId"];
    if (IsArrEmpty(arr)) return;
    NSString *newStr = [NSString stringWithFormat:@"[%@]", [arr componentsJoinedByString:@","]];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[JSTSystemInfo userKey] forKey:@"key"];
    [dict setValue:[JSTSystemInfo applicationIdentifier] forKey:@"pakname"];
    [dict setValue:newStr forKey:@"id"];
    [JSTHttpClient sendRequest:[NSString stringWithFormat:@"%@/%@", kAppServerForHttps, BackFilteredInterface] requestMethod:AFRequestMethodPost parameters:dict success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if (backFilteredDic) backFilteredDic(dic);
    } failure:^(NSUInteger statusCode, NSString *error) {
        if (errStr) errStr(error);
    }];
}

//上传广告悬停时间
+ (void) postAdvertMonitorStopTime:(NSDictionary *)stopTimeData success:(void(^)(NSDictionary *successDic))success err:(void(^)(NSString *errStr))errStr{
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:stopTimeData];
    [dict setValue:[JSTSystemInfo userKey] forKey:@"key"];
    [dict setValue:[JSTSystemInfo applicationIdentifier] forKey:@"pakname"];
    [JSTHttpClient sendRequest:[NSString stringWithFormat:@"%@/%@", kAppServerForHttps, StopTimeInterface] requestMethod:AFRequestMethodGet parameters:dict success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if(success)success(dic);
    } failure:^(NSUInteger statusCode, NSString *error) {
        if (errStr) errStr(error);
    }];
}

//APP安装量接口
+ (void) postAPPInstallDataSuccess:(void(^)(NSDictionary *successDic))success err:(void(^)(NSString *errStr))errStr{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[JSTSystemInfo userKey] forKey:@"key"];
    [dict setValue:[JSTSystemInfo applicationIdentifier] forKey:@"pakname"];
    [dict setValue:[JSTSystemInfo adid] forKey:@"idfa"];
    [dict setValue:[JSTSystemInfo createBSSID] forKey:@"mac"];
    [JSTHttpClient sendRequest:[NSString stringWithFormat:@"%@/%@", kAppServerForHttps, InstallInterface] requestMethod:AFRequestMethodPost parameters:dict success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if(success)success(dic);
    } failure:^(NSUInteger statusCode, NSString *error) {
        if(errStr)errStr(error);
    }];
}
//请求广告样式
+ (void) requestAdvertStyleData:(NSString *)advertId Success:(void (^)(NSDictionary *))success err:(void (^)(NSString *))errStr{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:advertId forKey:@"id"];
    [dict setValue:[JSTSystemInfo token] forKey:@"token"];
    [JSTHttpClient sendRequest:[NSString stringWithFormat:@"%@/%@", kAppServerForHttps, AdvertStyleInterface] requestMethod:AFRequestMethodGet parameters:dict success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        NSString *status = [NSString stringWithFormat:@"%@",dic[@"status"]];
        if ([status isEqualToString:@"-3"] || [status isEqualToString:@"-4"]) {
            [(AppDelegate *)[UIApplication sharedApplication].delegate postAppInfo];
        }
        if(success)success(dic);
    } failure:^(NSUInteger statusCode, NSString *error) {
        if(errStr)errStr(error);
    }];
}
//请求token参数
+ (void)requestAPPInfoDataSuccess:(void (^)(NSDictionary *))success err:(void (^)(NSString *))errStr{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSMutableDictionary *vDic = [NSMutableDictionary dictionaryWithDictionary:[JSTHttpClient uaDic]];
    NSData *data = [NSJSONSerialization dataWithJSONObject:vDic options:NSJSONWritingPrettyPrinted error:nil];
    NSString *dataStr = [[NSString alloc] initWithData:[GTMBase64 encodeData:data] encoding:NSUTF8StringEncoding];
    NSString *encryeptStr = [AES128_DawnzrXie AES128Encrype:dataStr];
    [dict setValue:encryeptStr forKey:@"v"];
    [dict setValue:@"1" forKey:@"versions"];
    [dict setValue:[JSTSystemInfo appVersion] forKey:@"ver"];
    [JSTHttpClient sendRequest:[NSString stringWithFormat:@"%@/%@", kAppServerForHttps, AppInfoInterface] requestMethod:AFRequestMethodGet parameters:dict success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if(success)success(dic);
    } failure:^(NSUInteger statusCode, NSString *error) {
        if(errStr)errStr(error);
    }];
    
}
//
+ (void) requestCPSInfoDataWithDict:(NSDictionary *)dic Success:(void(^)(NSDictionary *successDic))success err:(void(^)(NSString *errStr))errStr{
    NSMutableDictionary *parma = [NSMutableDictionary dictionaryWithDictionary:dic];
    parma[@"token"] = [JSTSystemInfo token];
    [JSTHttpClient sendRequest:[NSString stringWithFormat:@"%@/%@",kAppServerForHttps,CPSInterface] requestMethod:AFRequestMethodGet parameters:parma success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if (success) success(dic);
    } failure:^(NSUInteger statusCode, NSString *error) {
        if (errStr) errStr(error);
    }];
}
+ (NSString*) encodeString:(NSString*)unencodedString {
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              (CFStringRef)unencodedString,
                                                              NULL,
                                                              (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                              kCFStringEncodingUTF8));
    
    return encodedString;
}
+ (void)showAlertWithTitle:(NSString *)titleStr withDetailStr:(NSString *)detailStr andConfirmAction:(JSTAlertBlock)alertBlock {
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:titleStr message:detailStr preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        alertBlock();
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
    }];
    [alertC addAction:otherAction];
    [alertC addAction:cancelAction];
    [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:alertC animated:YES completion:nil];
}


@end
